/**
 * 权限校验:
 * Vue Router 的前置钩子函数 beforeEach(to,from,next)
 * to:即将要进入的目标路由对象
 * from：即将要离开的路由对象
 * next：
 */
import router from "./src/router";
import Cookies from 'js-cookie'
import store from "./store";
import Message from "ant-design-vue/lib/message";
import {whiteList, whitePath} from "./src/utils/white";

router.beforeEach((to, from, next) => {
    next()
    const token = Cookies.get("user_token_web")
    let jwtToken = sessionStorage.getItem("mm-user-token-web");
    if (!token || !jwtToken) {
        if (to.path === "/login" || !to.meta.auth) {
            next();
        } else {
            if (to.meta.auth&&!whitePath.includes(to.path)) {
                next({path: "/login"});
            } else {
                next()
            }
        }
    } else {
        if (to.path === "/login") {
            //从非登录页面到登录页面
            if (from.path === '/') {
                console.log(from.path)
                next({path: '/mall/index'})
            } else {
                console.log(from.path + "aa")
                Message.warning("请先退出", 2);

                next({path: from.path})

            }

        } else if (to.meta.role) {
            console.log(to)

            let loginUserInfo = sessionStorage.getItem("mm-login-user-web")
            if (!loginUserInfo) {
                next({path: "/login"})
            }
            loginUserInfo = JSON.parse(loginUserInfo)
            let roles=[]
            if (loginUserInfo.type.indexOf("_")>-1){
                roles = loginUserInfo.type.split("_");
            }else{
                roles[0]=loginUserInfo.type
            }
            if (to.meta.role==="web-enterprise"||to.meta.role==="web-shop"){
                if (roles.includes(to.meta.role)){
                    next()
                }else {
                    next({path:"/home"})
                }
            }else if (to.meta.role==="web-common"){
                if (roles.includes(to.meta.role)){
                    next()
                }else {
                    //企业用户
                    next({path: "/shop_apply_result"})
                }
            }else {
                next()
            }

        } else {
            next()
        }
    }


});
